let res = location.search.slice(4);

// console.log(res);

$.post("../goodsAndShoppingCart/getShoppingCart.php", {
    "vipName": res,
    // "$vipName":
}).then(function(data) {
    console.log(data)
    let res = JSON.parse(data)
    console.log(res)
        // $(".img-box img").attr("src", res[0].goodsImg);
        // $(".goods-info .text-1").html(res[0].goodsName)
        // $(".goods-info .text-2").html(res[0].beiyong4)
        // $(".shuxing").html(res[0].goodsType)
        // $(".price").html("￥" + res[0].goodsPrice)
        // $(".num-box input").val(res[0].goodsCount)


    let htmlStr = "";
    for (let i = 0; i < res.length; i++) {
        htmlStr += ` <tr>
                            <td class="goods-box">
                                <input class="check-icon1" type="checkbox">
                                <p><span class = "goodsid">${res[i].goodsId}</span></p>
                                <a href="" class="img-box">
                                    <img src="${res[i].goodsImg}" alt="">
                                </a>
                                <a href="" class="goods-info">
                                    <p class="text-1">${res[i].goodsName}</p>
                                    <p class="text-2">${res[i].beiyong4}</p>

                                </a>
                            </td>
                            <td>
                                <p class="shuxing">${res[i].goodsType}</p>
                            </td>
                            <td>
                                <p class="price">￥${res[i].goodsPrice}</p>
                            </td>
                            <td>
                                <div class="num-box">
                                    <span class="minus">-</span>
                                    <input type="tel"  class="goodsnum" value="${res[i].goodsCount}">
                                    <span class="add">+</span>
                                </div>
                            </td>
                            <td>
                                <p class="xiaoji">￥${res[i].goodsCount*res[i].goodsPrice}</p>
                            </td>
                            <td class="btn-box"><span class="f-btn">删除</span>
                                <span class="f-btn-1">移入收藏</span>
                            </td>
                        </tr>`;
    }
    $("tbody").html(htmlStr);
    //删除和修改
    for (let i = 0; i < $("tbody tr").length; i++) {
        $(".f-btn").eq(i).click(function() {
            let goodsId = res[i].goodsId;
            let username = localStorage.getItem("loginphone")

            $.get("../goodsAndShoppingCart/deleteGoods.php?goodsId=" + goodsId + "&vipName=" + username, function(deldata) {});
            $(this).parent().parent().remove();
        })

    }
    $(".add").click(function() {
        let count = Number($(this).parent().children().eq(1).val()) + 1
        $(this).parent().children().eq(1).val(count);
        getxiaoji();
        // zongjia1();
        counts()
    });
    $(".minus").click(function() {
        let count = $(this).parent().children().eq(1).val() - 1;
        // console.log(count)
        if (count < 1) {
            count = 1;
        }
        $(this).parent().children().eq(1).val(count);
        getxiaoji();
        // zongjia1();
        counts()
    });
    getAll();
    zongjia2();
    counts()
    $(".goodsnum").change(function() {
        let goodsId = $(".goodsid").html()
        let username = localStorage.getItem("loginphone")
        let goodsnum = $(".goodsnum").val();
        $.get("../goodsAndShoppingCart/updateGoodsCount.php?vipName=" + username + "&goodsId=" + goodsId + "&goodsCount=" + goodsnum, function(resText) {
            console.log(resText);
        });
        getxiaoji()
        counts()
            // zongjia2()

    })

});

function zongjia2() {
    for (let i = 0; i < $(".check-icon1").length; i++) {
        let index = i;
        $(".check-icon1").eq(index).click(function() {
            let xiaoji1 = Number($(".check-icon1").eq(i).parent().parent().find("td:eq(4)").find("p").html().slice(1));
            // console.log(xiaoji1)
            let zongjia = Number($(".text-box span").eq(1).html().slice(1))

            let zongjia1 = ""
            let zongjia2 = ""
                // console.log(zongjia)
            if ($(this).prop("checked")) {
                zongjia1 = zongjia + xiaoji1;
                // console.log(zongjia1)
                $(".text-box span").eq(1).html("￥" + zongjia1 + ".00");
                $(".sum-price .text-2").html("￥" + zongjia1 + ".00");


            } else {
                zongjia2 = Number($(".text-box span").eq(1).html().slice(1) - xiaoji1);
                // console.log(zongjia2)
                $(".text-box span").eq(1).html("￥" + zongjia2 + ".00")
                $(".sum-price .text-2").html("￥" + zongjia2 + ".00");


            }
        })

    }
}

function getAll() {
    //全选框
    $(".check-icon2").click(function() {
        var btn = $(".check-icon2").prop("checked");
        $(".check-icon1").prop("checked", btn);
        if (btn) {
            zongjia1();
        } else {
            $(".sum-price .text-2").html("￥" + "0.00");
            //商品金额
            $(".text-box .price").html("￥" + "0.00");
        }
    })

    $(".check-icon1").click(function() {
        var CL = $(".check-icon1").length; //列表长度；
        var CH = $(".check-icon1:checked").length; //列表中被选中的长度
        if (CL == CH) {
            $(".check-icon2").prop("checked", true);

        } else {
            $(".check-icon2").prop("checked", false);

        }

    })

}

//总数目

function counts() {
    let num = 0;
    for (let i = 0; i < $(".goodsnum").length; i++) {
        num = num + Number($('.goodsnum').eq(i).val())
        console.log(num)
    }
    $(".text-box span em").html(num)
}

function zongjia1() {
    let sum = 0;
    for (let i = 0; i < $(".goodsnum").length; i++) {
        // console.log(sum);
        let xiaoji = $(".xiaoji").eq(i).html().slice(1)
        sum = sum / 1 + xiaoji / 1;
        // console.log(sum);
        //总价格

    }

    $(".sum-price .text-2").html("￥" + sum + ".00");
    //商品金额
    $(".text-box .price").html("￥" + sum + ".00");

}

function getxiaoji() {
    let goodssingleprice = null;

    for (let i = 0; i < $(".goodsnum").length; i++) {
        let goodsnum = $(".goodsnum").eq(i).val();
        // console.log(goodsnum)
        let goodsprice = $("td .price").eq(i).html();
        // console.log(goodsprice)
        // console.log(goodsprice.slice(1))
        goodsprice = goodsprice.slice(1)
        goodssingleprice = goodsnum * goodsprice;
        // console.log(goodssingleprice)

        $(".xiaoji").eq(i).html("￥" + goodssingleprice);
        // console.log($(".xiaoji").eq(i).html())
    }

}